package com.example.yian.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Base64;
import java.util.Date;

public class JwtUtils {

    private static final String SECRET_KEY = "yourSecretKey-132132131326546123dsadasfdsadfdadadasdasdasdsdadsasdssadadsdasdasdwaddasasadsfdf";
    private static final String ENCODED_SECRET_KEY = Base64.getEncoder().encodeToString(SECRET_KEY.getBytes());
    public static String generateToken(String openid) {
        return Jwts.builder()
                .setSubject(openid)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 99L * 365 * 86400000)) // 1 day expiration
                .signWith(SignatureAlgorithm.HS256, ENCODED_SECRET_KEY)
                .compact();
    }

    public static Claims parseToken(String token) {
        return Jwts.parser()
                .setSigningKey(ENCODED_SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();
    }
}
